package com.zc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zc.Enum.ErrorEnum;
import com.zc.dto.LoginDto;
import com.zc.mapper.SysAccountMapper;
import com.zc.pojo.SysAccount;
import com.zc.service.SysAccountService;
import com.zc.utils.JSONResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SysAccountServiceImpl implements SysAccountService {

    @Autowired
    private SysAccountMapper sysAccountMapper;


    @Override
    public JSONResult login(String username, String password) {
        LoginDto loginDTO = new LoginDto();
        loginDTO.setPath("redirect:/");//重定向

        SysAccount sysAccount = new SysAccount();
        sysAccount.setUsername(username);
        sysAccount = sysAccountMapper.selectOne(sysAccount);
/*
        //校验账号
        if (BeanUtil.isEmpty(sysAccount)) {
            return JSONResult.errorMsg(ErrorEnum.B_10005, loginDTO);
        }

        //校验密码
        MD5 md5 = new MD5(sysAccount.getSalt().getBytes());
        String digestHex = md5.digestHex(password);
        if(!StringUtils.equals(digestHex,sysAccount.getPassword())){
            return JSONResult.errorMsg(ErrorEnum.B_10006, loginDTO);
        }*/

        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            subject.login(token);//执行登录的方法，如果没有异常就说明ok
        } catch (UnknownAccountException e) {     //用户名不存在
            e.printStackTrace();
            return JSONResult.errorMsg(ErrorEnum.B_10005, loginDTO);
        } catch (IncorrectCredentialsException e) {//密码错误
            e.printStackTrace();
            return JSONResult.errorMsg(ErrorEnum.B_10006, loginDTO);
        }
        loginDTO.setPath("login/main");//进入主页
        loginDTO.setSysAccount(sysAccount);

        return JSONResult.ok(loginDTO);
    }

    @Override
    public PageInfo<SysAccount> queryByPage(String realName, String email, String startDate, String endDate, Integer page, Integer limit) {

        Map<String, Object> map = new HashMap<>();
        map.put("realName", realName);
        map.put("email", email);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        PageHelper.startPage(page, limit);
        List<SysAccount> sysAccountList = sysAccountMapper.listSysAccount(map);
        PageInfo<SysAccount> pageInfo = new PageInfo<SysAccount>(sysAccountList);

        return pageInfo;
    }

    @Override
    public void save(SysAccount sysAccount) {
        sysAccount.setDeleted(new Byte("0"));
        sysAccount.setCreateTime(new Date());
        sysAccountMapper.insert(sysAccount);
    }

    @Override
    public SysAccount getById(Long accountId) {
        return sysAccountMapper.selectByPrimaryKey(accountId);
    }

    @Override
    public SysAccount getByUsername(String username) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        return sysAccountMapper.getByUsername(map);
    }

    @Override
    public void update(SysAccount sysAccount) {
        sysAccount.setDeleted(new Byte("0"));
        sysAccount.setCreateTime(new Date());
        sysAccount.setModifiedTime(new Date());
        sysAccountMapper.updateByPrimaryKey(sysAccount);
    }

    @Override
    public void delete(Long accountId) {
        sysAccountMapper.deleteByPrimaryKey(accountId);
    }

    @Override
    public void logicalDelete(Long accountId) {
        SysAccount sysAccount = new SysAccount();
        sysAccount.setDeleted(new Byte("1"));
        sysAccount.setModifiedTime(new Date());
        sysAccount.setAccountId(accountId);
        sysAccountMapper.logicalDelete(sysAccount);
    }
}